package com.it.design.strategWithOutDesignMode;

import com.it.design.withOutFacotryDesignMode.WithOutFacotryDesignMode;
import com.sun.javafx.image.BytePixelSetter;

/*
 * 策略模式
 * */
public class StrategWithOutDesignMode {
    public static void main(String[] args) {
//比如有一个优惠券系统、然后这个优惠券系统有很多种优惠策略
        int discount = 2;
        DiscountCaculateStrategy discountCaculateStrategy = DiscountCalcalateStraterryFacotry.changeStrategy(discount);
        discountCaculateStrategy.calcalate();
////        太多if - else if。。。。可能导致后期代码逻辑理不清
//        可以通过策略模式优化
//        int descount = 0;
//        if (descount == 1) {
//            System.out.println("复杂业务逻辑1");
//        } else if (descount == 2) {
//            System.out.println("复杂业务逻辑2");
//        } else if (descount == 3) {
//            System.out.println("复杂业务逻辑3");
//        } else {
//            System.out.println("复杂业务逻辑4");
//        }
    }

    public interface DiscountCaculateStrategy {
        void calcalate();
    }

    public static class DiscountCaculateStrategyA implements DiscountCaculateStrategy {
        @Override
        public void calcalate() {
            System.out.println("复杂的优惠计算策略1");
        }
    }

    public static class DiscountCaculateStrategyB implements DiscountCaculateStrategy {
        @Override
        public void calcalate() {
            System.out.println("复杂的优惠计算策略2");
        }
    }

    public static class DiscountCaculateStrategyC implements DiscountCaculateStrategy {
        @Override
        public void calcalate() {
            System.out.println("复杂的优惠计算策略3");
        }
    }

    public static class DiscountCaculateStrategyD implements DiscountCaculateStrategy {
        @Override
        public void calcalate() {
            System.out.println("复杂的优惠计算策略4");
        }
    }

    public static class DiscountCaculateStrategyE implements DiscountCaculateStrategy {
        @Override
        public void calcalate() {
            System.out.println("复杂的优惠计算策略5");
        }
    }

    //工厂
    public static class DiscountCalcalateStraterryFacotry {

        public static DiscountCaculateStrategy changeStrategy(int discount) {
            if (discount == 1) {
                return new DiscountCaculateStrategyA();
            } else if (discount == 2) {
                return new DiscountCaculateStrategyB();
            } else if (discount == 3) {
                return new DiscountCaculateStrategyC();
            } else if (discount == 4) {
                return new DiscountCaculateStrategyD();
            } else {
                return new DiscountCaculateStrategyE();
            }
        }
    }
}
